package rollercoaster.railway.utils;

import rollercoaster.commons.Vertex3D;

public class VertexUtils {
		
	
	public static Vertex3D meanPoint(Vertex3D P1, Vertex3D P2){
		return new Vertex3D((P2.getX()+P1.getX())/2.0f, (P2.getY()+P1.getY())/2.0f, (P2.getZ()+P1.getZ())/2.0f);
	}
	
	

	public static void normalizeVector(Vertex3D vector){
		
		float[] coord = {vector.getX(),vector.getY(),vector.getZ()};
		float norm = 0;
		for (int i = 0; i < coord.length; i++) {
			norm+=coord[i]*coord[i];
		}
		
		norm = (float) Math.sqrt(norm);
		for (int i = 0; i < coord.length; i++) {
			coord[i] = coord[i]/norm;
		}
		
		vector.setX(coord[0]);
		vector.setY(coord[1]);
		vector.setZ(coord[2]);
	}
	
	public static Vertex3D crossProduct(Vertex3D v1, Vertex3D v2){
		
		float a=v1.getX();
		float b=v1.getY();
		float c=v1.getZ();
		
		float d=v2.getX();
		float e=v2.getY();
		float f=v2.getZ();
		
	
		return new Vertex3D(b*f-c*e, c*d-a*f, a*e-b*d);
		
	}
	
	public static float distance(Vertex3D v1, Vertex3D v2){
		float mod=(v1.getX()-v2.getX())*(v1.getX()-v2.getX())+(v1.getY()-v2.getY())*(v1.getY()-v2.getY())+(v1.getZ()-v2.getZ())*(v1.getZ()-v2.getZ());
		return (float) Math.sqrt(mod);
	}
	
	
}
